package com.technology.education.dslitem

import android.widget.LinearLayout
import android.widget.TextView
import com.angcyo.dsladapter.DslAdapterItem
import com.angcyo.dsladapter.DslViewHolder
import com.technology.education.R
import com.technology.education.dslitem.style.BaseStyle
import com.technology.education.dslitem.style.DrawableStyle
import com.technology.education.ext.getColor
import com.technology.education.ext.getResId
import me.hgj.jetpackmvvm.ext.util.dp2px

/**
 *@author:create by BZY
 *Date: 2020/8/4 17
 */
class ItemColumnTitleText(
    var title: String? = "",
    var content: String? = "",
    var tag: String? = "",
    var style: BaseStyle = BaseStyle(),
    var titleDrawable: DrawableStyle? = null,
    var contentDrawable: DrawableStyle? = null,
    var hintStr: String? = ""
) : DslAdapterItem() {
    init {
        itemLayoutId = R.layout.item_column_title_text
    }

    override fun onItemBind(
        itemHolder: DslViewHolder,
        itemPosition: Int,
        adapterItem: DslAdapterItem,
        payloads: List<Any>
    ) {
        super.onItemBind(itemHolder, itemPosition, adapterItem, payloads)
        itemTag = tag
        itemHolder.v<TextView>(R.id.tv_title)?.apply {
            text = title
            textSize = style.titleTextSize
            paint.isFakeBoldText = style.isBold
            setTextColor(getColor(style.titleTextColor))
            gravity = style.titleGravity
            if (titleDrawable != null) {
                titleDrawable?.let {
                    setCompoundDrawablesWithIntrinsicBounds(
                        it.leftDrawable,
                        it.topDrawable,
                        it.rightDrawable,
                        it.bottomDrawable
                    )
                    compoundDrawablePadding = dp2px(it.drawablePadding)
                }
            } else {
                setCompoundDrawablesWithIntrinsicBounds(
                    0,
                    0,
                    0,
                    0
                )
            }
        }
        itemHolder.v<TextView>(R.id.tv_content)?.apply {
            hint = hintStr
            text = content
            textSize = style.contentTextSize
            setTextColor(getColor(style.contentTextColor))
            gravity = style.contentGravity
            if (contentDrawable != null) {
                contentDrawable?.let {
                    setCompoundDrawablesWithIntrinsicBounds(
                        it.leftDrawable,
                        it.topDrawable,
                        it.rightDrawable,
                        it.bottomDrawable
                    )
                    compoundDrawablePadding = dp2px(it.drawablePadding)
                }
            } else {
                setCompoundDrawablesWithIntrinsicBounds(
                    0,
                    0,
                    0,
                    0
                )
            }
        }
        itemHolder.v<LinearLayout>(R.id.ll_parent)?.apply {
            setPadding(
                dp2px(style.leftPadding), dp2px(style.topPadding), dp2px(style.rightPadding),
                dp2px(style.bottomPadding)
            )
            if (style.itemBackColor > 0) {
                setBackgroundColor(getColor(style.itemBackColor))
            } else {
                setBackgroundColor(0)
                background =
                    if (style.itemBackDrawable > 0) getResId(style.itemBackDrawable) else null
            }
            gravity = style.parentGravity
        }
        itemHolder.clickItem(_clickListener)
        itemHolder.click(R.id.tv_content, _clickListener)
        if (style.itemHeight > 0) {
            itemHeight = style.itemHeight
        }
        itemSpanCount = style.itemSpan
        itemTopInsert = style.topLine
        itemBottomInsert = style.lineHeight
        itemDecorationColor = getColor(style.lineColor)
        itemLeftOffset = style.leftOffset
        itemRightOffset = style.rightOffset
        itemTopOffset = style.topOffset
        itemBottomOffset = style.bottomOffset
    }
}